package com.yuncheng.spcyApi.controller;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.bo.ParamsBo;
import com.yuncheng.spcyApi.constant.common.BusinessConstant;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.other.PxSjConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.constant.wjml.WjmlConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.spcyApi.mapper.SpcyWjWjjbxxMapper;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.utils.*;
import com.yuncheng.common.base.controller.BaseController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.vo.common.StartEndTimeVo;
import com.yuncheng.spcyApi.vo.wj.TreeWjtmVo;
import com.yuncheng.spcyApi.vo.wj.WjtmStatVo;
import com.yuncheng.spcyApi.vo.wj.WjtmVo;
import com.yuncheng.spcyApi.vo.wj.WjxxDbtVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import utils.*;

/**
 * @Description: 审评查验-问卷基本信息
 * @Author: yuncheng
 * @Date: 2023-05-05
 * @Version: V1.0
 */
@Api(tags = "审评查验-问卷基本信息接口")
@RestController
@RequestMapping("/api/spzx-spcy/spcyWjWjjbxx")
public class SpcyWjWjjbxxController extends BaseController<SpcyWjWjjbxx, ISpcyWjWjjbxxService> {

    private static final Logger log = LoggerFactory.getLogger(SpcyWjWjjbxxController.class);

    @Resource
    private ISpcyWjWjjbxxService spcyWjWjjbxxService;

    @Resource
    private ISpcyWjWjtmService spcyWjWjtmService;

    @Resource
    private ISpcyWjWjtmxxService spcyWjWjtmxxService;

    @Resource
    private ISpcyWjWjtxjbxxService spcyWjWjtxjbxxService;

    @Resource
    private ISpcyWjWjtxtmService spcyWjWjtxtmService;

    @Resource
    private ISpcyWjWjtxtmxxService spcyWjWjtxtmxxService;

    @Resource
    private SpcyWjWjjbxxMapper spcyWjWjjbxxMapper;

    @Resource
    private ISpcyWjDbtService spcyWjDbtService;

    @Resource
    private ISpcyLogService spcyLogService;

    @Resource
    private MyExcelUtils myExcelUtils;

    @Resource
    private BusinessConstant businessConstant;

    @Resource
    private LocalFileUploadUtils localFileUploadUtils;


    @Log(title = "问卷基本信息-分页列表查询")
    @ApiOperation(value = "问卷基本信息-分页列表查询")
    @GetMapping(value = "/wjjbxxPageList")
    public HttpResult<?> wjjbxxPageList(SpcyWjWjjbxx wjWjjbxx,
                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SpcyWjWjjbxx> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<SpcyWjWjjbxx> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(wjWjjbxx.getFwjbt()),SpcyWjWjjbxx::getFwjbt,wjWjjbxx.getFwjbt());
        queryWrapper.like(StringUtils.isNotEmpty(wjWjjbxx.getFwjlx()),SpcyWjWjjbxx::getFwjlx,wjWjjbxx.getFwjlx());
        queryWrapper.like(StringUtils.isNotEmpty(wjWjjbxx.getFwjms()),SpcyWjWjjbxx::getFwjms,wjWjjbxx.getFwjms());

        // 2023-6-26 新增
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser != null) {
            if (!SpcyConstant.Org_Code_Admin.equals(currentUser.getDeptCode())){
                queryWrapper.eq(SpcyWjWjjbxx::getSysOrgCode, currentUser.getDeptCode());
            }
        }

        queryWrapper.orderByDesc(SpcyWjWjjbxx::getCreateTime);
        IPage<SpcyWjWjjbxx> pageList = spcyWjWjjbxxService.page(page, queryWrapper);
        return HttpResult.ok(pageList);
    }

    @Log(title = "填写的问卷基本信息-分页列表查询")
    @ApiOperation(value = "填写的问卷基本信息-分页列表查询")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "flag",value = "是（已提交的问卷）/ 否")
    })
    @GetMapping(value = "/txwjjbxxPageList")
    public HttpResult<?> txwjjbxxPageList(SpcyWjWjtxjbxx spcyPxDjjbxx,
                                          StartEndTimeVo startEndTimeVo,
                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SpcyWjWjtxjbxx> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<SpcyWjWjtxjbxx> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxDjjbxx.getFbz()),SpcyWjWjtxjbxx::getFbz,spcyPxDjjbxx.getFbz());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxDjjbxx.getFwjbt()),SpcyWjWjtxjbxx::getFwjbt,spcyPxDjjbxx.getFwjbt());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxDjjbxx.getFwjjbxxid()),SpcyWjWjtxjbxx::getFwjjbxxid,spcyPxDjjbxx.getFwjjbxxid());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxDjjbxx.getFwjlx()),SpcyWjWjtxjbxx::getFwjlx,spcyPxDjjbxx.getFwjlx());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxDjjbxx.getFwjms()),SpcyWjWjtxjbxx::getFwjms,spcyPxDjjbxx.getFwjms());
        // 2023-6-26 新增
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser != null) {
            if (!SpcyConstant.Org_Code_Admin.equals(currentUser.getDeptCode())){
                queryWrapper.eq(SpcyWjWjtxjbxx::getSysOrgCode, currentUser.getDeptCode());
            }
        }

        if (startEndTimeVo.getStartTime() != null && startEndTimeVo.getEndTime() != null){
            queryWrapper.between(SpcyWjWjtxjbxx::getFtjrq,startEndTimeVo.getStartTime(),startEndTimeVo.getEndTime());
        }
        if (StringUtils.isNotBlank(startEndTimeVo.getFlag())) {
            if (startEndTimeVo.getFlag().equals("是")) {
                queryWrapper.isNotNull(SpcyWjWjtxjbxx::getFtjrq);
            } else {
                queryWrapper.isNull(SpcyWjWjtxjbxx::getFtjrq);
            }
        }

        if (spcyPxDjjbxx.getFtxrq() != null){
            queryWrapper.apply("to_char(ftxrq,'yyyy-mm-dd') >= {0}",DateConversionUtils.DateToStringYYYYMMDD(spcyPxDjjbxx.getFtxrq()));
        }
        if (spcyPxDjjbxx.getFtjrq() != null){
            queryWrapper.apply("to_char(ftjrq,'yyyy-mm-dd') >= {0}",DateConversionUtils.DateToStringYYYYMMDD(spcyPxDjjbxx.getFtjrq()));
        }
//        queryWrapper.orderByDesc(SpcyWjWjtxjbxx::getFtjrq);
//        queryWrapper.orderByDesc(SpcyWjWjtxjbxx::getCreateTime);
        queryWrapper.orderByDesc(SpcyWjWjtxjbxx::getFtxrq);
        IPage<SpcyWjWjtxjbxx> pageList = spcyWjWjtxjbxxService.page(page, queryWrapper);
        return HttpResult.ok(pageList);
    }

    @Log(title = "查询现场检查回访记录表")
    @ApiOperation(value = "查询现场检查回访记录表")
    @GetMapping(value = "/queryXcjcHfjlb")
    public HttpResult<?> queryXcjcHfjlb() {
        LambdaQueryWrapper<SpcyWjWjjbxx> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcyWjWjjbxx::getFwjlx, PxSjConstant.WJLX_XCJC_HFJLB);
        queryWrapper.orderByAsc(SpcyWjWjjbxx::getCreateTime);
        List<SpcyWjWjjbxx> pageList = spcyWjWjjbxxService.list(queryWrapper);
        String wjid = "";
        if (CollectionUtil.isNotEmpty(pageList)){
            wjid = pageList.get(0).getId();
        }

        Map map = new HashMap();
        map.put("wjid", wjid);
        return HttpResult.ok(map);
    }

    @Log(title = "问卷基本信息-添加")
    @ApiOperation(value = "问卷基本信息-添加")
    @PostMapping(value = "/addWjjbxx")
    public HttpResult<?> addWjjbxx(@RequestBody SpcyWjWjjbxx wjjbxx) {
        String sjId = "";
        if (StringUtils.isNotBlank(wjjbxx.getId())){
            sjId = wjjbxx.getId();
            SpcyWjWjjbxx oldSjjbxx = spcyWjWjjbxxService.getById(sjId);
            spcyWjWjjbxxService.updateById(wjjbxx);

            String operContent = "问卷基本信息修改：";
            Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(oldSjjbxx);
            operContent += LogUtils.BeanModifyMsg(wjjbxx,oldMap);

            spcyLogService.addLogs(LogConstant.LOGS_WJ_WJJBXX,operContent,sjId,LogConstant.LOGS_WJ_WJJBXX, SpcyConstant.LOGS_SYS_TYPE_XG);
        }else {
            sjId = GetUuIdUtils.ReplaceUuId();
            SpcyWjWjjbxx sjjbxx = new SpcyWjWjjbxx();
            BeanUtil.copyProperties(wjjbxx,sjjbxx);
            sjjbxx.setId(sjId);
            sjjbxx.setFyxq(DateConversionUtils.convertStringToDateYmd("2026-07-13"));
            spcyWjWjjbxxService.save(sjjbxx); // 新增基本问卷信息

            String operContent = "新增问卷基本信息：";
            // 将map里面的属性和值转String
            Map<String, Object> beanMap = ObjectToMapUtils.objectToMap(sjjbxx);
            operContent += LogUtils.MapToStringMethod(beanMap);
            spcyLogService.addLogs(LogConstant.LOGS_WJ_WJJBXX,operContent,sjId, LogConstant.LOGS_WJ_WJJBXX, SpcyConstant.LOGS_SYS_TYPE_XZ);
        }
        SpcyWjWjjbxx sjjbxx = spcyWjWjjbxxService.getById(sjId);
        return HttpResult.ok(sjjbxx);
    }


    @Log(title = "问卷基本信息-编辑")
    @ApiOperation(value = "问卷基本信息-编辑")
    @ApiImplicitParam(name = "id",value = "问卷基本信息id",required = true)
    @PutMapping(value = "/editWjjbxx")
    public HttpResult<?> editWjjbxx(@RequestBody HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException{
        if (StringUtils.isEmpty(buffer.get("id"))){
            return HttpResult.error("问卷基本信息id不能为空");
        }
        String sjjbxxId = buffer.get("id");
//        SpcyWjWjjbxx oldSjjbxx = spcyWjWjjbxxService.getById(sjjbxxId);
        SpcyWjWjjbxx oldSjjbxxUpdate = spcyWjWjjbxxService.getById(sjjbxxId);
        SpcyWjWjjbxx oldSjjbxx = oldSjjbxxUpdate;
        if (oldSjjbxxUpdate == null){
            return HttpResult.error("当前问卷基本信息数据不存在，请重试");
        }

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(oldSjjbxxUpdate, buffer);
        spcyWjWjjbxxService.updateById(oldSjjbxxUpdate);

        String operContent = "问卷基本信息修改：";
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(oldSjjbxx);
        operContent += LogUtils.BeanModifyMsg(oldSjjbxxUpdate,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_WJ_WJJBXX,operContent,sjjbxxId,LogConstant.LOGS_WJ_WJJBXX,SpcyConstant.LOGS_SYS_TYPE_XG);
        return HttpResult.ok("修改成功");
    }


    @Log(title = "问卷基本信息-批量删除")
    @ApiOperation(value = "问卷基本信息-批量删除")
    @DeleteMapping(value = "/deleteBatchWjjbxx")
    public HttpResult<?> deleteBatchWjjbxx(@RequestParam("ids") String ids) {
        if (StringUtils.isEmpty(ids)){
            return HttpResult.error("ids参数不能为空");
        }
        List<String> idsList = Arrays.asList(ids.split(","));
        List<SpcyWjWjjbxx> spcyRsGcyList = spcyWjWjjbxxService.listByIds(idsList);
        if (CollectionUtils.isEmpty(spcyRsGcyList)){
            return HttpResult.error("数据不存在，请重试");
        }
        String operContent = "";
        for (SpcyWjWjjbxx sjjbxx : spcyRsGcyList){
            String id = sjjbxx.getId(); // 试卷id

            // 删除大标题
            spcyWjDbtService.remove(new LambdaQueryWrapper<SpcyWjDbt>().eq(SpcyWjDbt::getFwjjbxxid, id));

            // 删除问卷题目 和 选项
            LambdaQueryWrapper<SpcyWjWjtm> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SpcyWjWjtm::getFwjjbxxid, id);
            List<SpcyWjWjtm> sjtmList = spcyWjWjtmService.list(queryWrapper);

            spcyWjWjjbxxService.removeById(id); // 删除问卷

            operContent = "删除问卷信息：";
            Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(sjjbxx);
            operContent += LogUtils.MapToStringMethod(oldMap);
            spcyLogService.addLogs(LogConstant.LOGS_WJ_WJJBXX,operContent,id,LogConstant.LOGS_WJ_WJJBXX,SpcyConstant.LOGS_SYS_TYPE_SC);
            operContent = "";

            // 不为空
            if (CollectionUtils.isNotEmpty(sjtmList)){
                for (SpcyWjWjtm sjtm : sjtmList){
                    String sjtmId = sjtm.getId();
                    spcyWjWjtmService.removeById(sjtm.getId()); // 删除问卷题目
                    spcyWjWjtmxxService.remove(new LambdaQueryWrapper<SpcyWjWjtmxx>()
                            .eq(SpcyWjWjtmxx::getFwjtmid, sjtmId)); // 删除选项
                }
            }
        }

        return HttpResult.ok("删除成功");
    }

    @Log(title = "问卷基本信息-通过id查询")
    @ApiOperation(value = "问卷基本信息-通过id查询")
    @ApiImplicitParam(value = "id",name = "问卷id",required = true)
    @GetMapping(value = "/queryWjjbxxById")
    public HttpResult<?> queryWjjbxxById(@RequestParam("id") String id) {
        SpcyWjWjjbxx spcyPxSjjbxx = spcyWjWjjbxxService.getById(id);
        return HttpResult.ok(spcyPxSjjbxx);
    }

    /*********************【 问卷题目 】**********************/

    @Log(title = "问卷题目-分页列表查询")
    @ApiOperation(value = "问卷题目-分页列表查询")
    @GetMapping(value = "/wjtmPageList")
    public HttpResult<?> wjtmPageList(SpcyWjWjtm spcyPxSjtm, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        /*Page<SpcyWjWjtm> page = new Page<>(pageNo, pageSize);
        IPage<SpcyWjWjtm> pageList = new Page<>();
        LambdaQueryWrapper<SpcyWjWjtm> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(spcyPxSjtm.getFwjjbxxid())) {
            queryWrapper.like(SpcyWjWjtm::getFwjjbxxid, spcyPxSjtm.getFwjjbxxid());
            queryWrapper.like(StringUtils.isNotEmpty(spcyPxSjtm.getFwjtm()), SpcyWjWjtm::getFwjtm, spcyPxSjtm.getFwjtm());
            queryWrapper.like(StringUtils.isNotEmpty(spcyPxSjtm.getFwjtmdbt()), SpcyWjWjtm::getFwjtmdbt, spcyPxSjtm.getFwjtmdbt());
            queryWrapper.like(StringUtils.isNotEmpty(spcyPxSjtm.getFwjtmlx()), SpcyWjWjtm::getFwjtmlx, spcyPxSjtm.getFwjtmlx());
            queryWrapper.orderByAsc(SpcyWjWjtm::getFxh);
            queryWrapper.orderByAsc(SpcyWjWjtm::getCreateTime);
            pageList = spcyWjWjtmService.page(page, queryWrapper);
        }*/
        Map resultMap = new HashMap();
        List<Map> resultList = new ArrayList<>();

        if (StringUtils.isBlank(spcyPxSjtm.getFwjjbxxid())) {
            return HttpResult.ok(resultList);
        }

        // 大标题
        List<SpcyWjDbt> dbtList = spcyWjDbtService.list(new LambdaQueryWrapper<SpcyWjDbt>()
                .eq(SpcyWjDbt::getFwjjbxxid, spcyPxSjtm.getFwjjbxxid())
                .orderByAsc(SpcyWjDbt::getForder));

        LambdaQueryWrapper<SpcyWjWjtm> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcyWjWjtm::getFwjjbxxid, spcyPxSjtm.getFwjjbxxid());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxSjtm.getFwjtm()), SpcyWjWjtm::getFwjtm, spcyPxSjtm.getFwjtm());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxSjtm.getFwjtmdbt()), SpcyWjWjtm::getFwjtmdbt, spcyPxSjtm.getFwjtmdbt());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxSjtm.getFwjtmlx()), SpcyWjWjtm::getFwjtmlx, spcyPxSjtm.getFwjtmlx());
        queryWrapper.orderByAsc(SpcyWjWjtm::getFxh);
        queryWrapper.orderByAsc(SpcyWjWjtm::getCreateTime);
        List<SpcyWjWjtm> wjtmList = spcyWjWjtmService.list(queryWrapper);

        if (CollectionUtils.isEmpty(dbtList)){
            return HttpResult.ok(resultList);
        }
        TreeWjtmVo treeDbtVo = null;
        for (SpcyWjDbt dbt : dbtList){
            treeDbtVo = new TreeWjtmVo();
            treeDbtVo.setId(dbt.getId());
            treeDbtVo.setFwjtmdbt(dbt.getFdbt());
            resultList.add(MapUtils.objectToMap(treeDbtVo));

            if (CollectionUtils.isNotEmpty(wjtmList)) {
                TreeWjtmVo treeWjtmVo = null;
                for (SpcyWjWjtm wjtm : wjtmList) {
                    if (dbt.getFdbt().equals(wjtm.getFwjtmdbt())) {
                        treeWjtmVo = new TreeWjtmVo();
                        BeanUtil.copyProperties(wjtm, treeWjtmVo);
                        treeWjtmVo.setParentid(dbt.getId());
                        resultList.add(MapUtils.objectToMap(treeWjtmVo));
                    }
                }
            }
        }

        resultMap.put("records",resultList);
        resultMap.put("total",resultList.size());
        return HttpResult.ok(resultMap);
    }


    @Log(title = "问卷题目-添加")
    @ApiOperation(value = "问卷题目-添加")
    @PostMapping(value = "/addWjtm")
    public HttpResult<?> addWjtm(@RequestBody SpcyWjWjtm spcyPxSjtm) {
        if (StringUtils.isEmpty(spcyPxSjtm.getFwjjbxxid())){
            return HttpResult.error("问卷基本信息id不能为空");
        }
        if (StringUtils.isBlank(spcyPxSjtm.getFwjtmdbt())){
            return HttpResult.error("问卷大标题不能为空");
        }
        if (StringUtils.isBlank(spcyPxSjtm.getFwjtm())){
            return HttpResult.error("问卷标题不能为空");
        }
        String sjid = spcyPxSjtm.getFwjjbxxid();
        List<SpcyWjWjtm> sjtmList = spcyWjWjtmService.list(new LambdaQueryWrapper<SpcyWjWjtm>()
                .eq(SpcyWjWjtm::getFwjjbxxid, sjid));
        int size = sjtmList.size();
        size = size + 1;
        spcyPxSjtm.setFxh(size);
        if (StringUtils.isBlank(spcyPxSjtm.getFwjtmlx())){
            spcyPxSjtm.setFwjtmlx(PxSjConstant.SJTM_LX_DANX); // 单选
        }
        if (StringUtils.isBlank(spcyPxSjtm.getFsfczxx())){
            spcyPxSjtm.setFsfczxx("是");
        }
        if (StringUtils.isBlank(spcyPxSjtm.getFsftxczkn())){
            spcyPxSjtm.setFsftxczkn("否");
        }
        if (StringUtils.isBlank(spcyPxSjtm.getFsftxyjjy())){
            spcyPxSjtm.setFsftxyjjy("否");
        }
        if (StringUtils.isBlank(spcyPxSjtm.getFsftxyxdz())){
            spcyPxSjtm.setFsftxyxdz("否");
        }
        if (StringUtils.isBlank(spcyPxSjtm.getFsftxxdyj())){
            spcyPxSjtm.setFsftxxdyj("否");
        }

        spcyWjWjtmService.save(spcyPxSjtm);
        String operContent = "新增问卷题目信息：";

        // 将map里面的属性和值转String
        Map<String, Object> beanMap = ObjectToMapUtils.objectToMap(spcyPxSjtm);
        operContent += LogUtils.MapToStringMethod(beanMap);
        spcyLogService.addLogs(LogConstant.LOGS_WJ_WJJBXX,operContent,spcyPxSjtm.getId(), LogConstant.LOGS_WJ_WJJBXX, SpcyConstant.LOGS_SYS_TYPE_XZ);
        return HttpResult.ok("添加成功");
    }

    @Log(title = "问卷题目-编辑")
    @ApiOperation(value = "问卷题目-编辑")
    @ApiImplicitParam(name = "id",value = "问卷题目id",required = true)
    @PutMapping(value = "/editWjtm")
    public HttpResult<?> editWjtm(@RequestBody HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException{
        if (StringUtils.isEmpty(buffer.get("id"))){
            return HttpResult.error("问卷题目id不能为空");
        }
        String sjtmId = buffer.get("id");
//        SpcyWjWjtm oldSjtm = spcyWjWjtmService.getById(sjtmId);
        SpcyWjWjtm oldSjtmUpdate = spcyWjWjtmService.getById(sjtmId);
        SpcyWjWjtm oldSjtm = oldSjtmUpdate;
        if (oldSjtmUpdate == null){
            return HttpResult.error("当前问卷题目基本信息数据不存在，请重试");
        }

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(oldSjtmUpdate, buffer);
        spcyWjWjtmService.updateById(oldSjtmUpdate);

        String operContent = "问卷题目基本信息修改：";
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(oldSjtm);
        operContent += LogUtils.BeanModifyMsg(oldSjtmUpdate,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_WJ_WJJBXX,operContent,sjtmId,LogConstant.LOGS_WJ_WJJBXX,SpcyConstant.LOGS_SYS_TYPE_XG);
        return HttpResult.ok("修改成功");
    }

    @Log(title = "问卷题目-批量删除")
    @ApiOperation(value = "问卷题目-批量删除")
    @DeleteMapping(value = "/deleteBatchWjtm")
    public HttpResult<?> deleteBatchWjtm(@RequestParam("ids") String ids) {
        if (StringUtils.isEmpty(ids)){
            return HttpResult.error("ids参数不能为空");
        }
        List<String> idsList = Arrays.asList(ids.split(","));
        List<SpcyWjWjtm> spcyRsGcyList = spcyWjWjtmService.listByIds(idsList);
        if (CollectionUtils.isEmpty(spcyRsGcyList)){
            return HttpResult.error("数据不存在，请重试");
        }
        String operContent = "";
        for (SpcyWjWjtm sjtm : spcyRsGcyList){
            String sjtmId = sjtm.getId(); // 问卷题目id
            spcyWjWjtmService.removeById(sjtmId);

            operContent = "删除问卷题目信息：";
            Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(sjtm);
            operContent += LogUtils.MapToStringMethod(oldMap);
            spcyLogService.addLogs(LogConstant.LOGS_WJ_WJJBXX,operContent,sjtmId,LogConstant.LOGS_WJ_WJJBXX,SpcyConstant.LOGS_SYS_TYPE_SC);
            operContent = "";

            // 删除题目选项
            spcyWjWjtmxxService.remove(new LambdaQueryWrapper<SpcyWjWjtmxx>()
                    .eq(SpcyWjWjtmxx::getFwjtmid, sjtmId));
        }

        return HttpResult.ok("删除成功");
    }

    /** 复制问卷题目及选项 */
    @GetMapping(value = "copyWjtm")
    public HttpResult copyWjtm(@RequestParam(value = "id",defaultValue = "")String id){
        SpcyWjWjtm wjtm = spcyWjWjtmService.getById(id);
        if (wjtm == null){
            return HttpResult.error("问卷题目为空！");
        }

        // 复制题目
        SpcyWjWjtm copyWjtm = new SpcyWjWjtm();
        BeanUtil.copyProperties(wjtm, copyWjtm);
        copyWjtm.setId(GetUuIdUtils.ReplaceUuId());
        copyWjtm.setFxh(wjtm.getFxh()+1);

        // 复制选项
        List<SpcyWjWjtmxx> copyWtxxList = new ArrayList<>();
        List<SpcyWjWjtmxx> xxList = spcyWjWjtmxxService.list(new LambdaQueryWrapper<SpcyWjWjtmxx>()
                .eq(SpcyWjWjtmxx::getFwjtmid, id));
        if (CollectionUtil.isNotEmpty(xxList)){
            for (SpcyWjWjtmxx wtxx : xxList){
                SpcyWjWjtmxx copyWjxx = new SpcyWjWjtmxx();
                BeanUtil.copyProperties(wtxx, copyWjxx);
                copyWjxx.setId(GetUuIdUtils.ReplaceUuId());
                copyWjxx.setFwjtmid(copyWjtm.getId());

                copyWtxxList.add(copyWjxx);
            }
        }

        spcyWjWjtmService.save(copyWjtm);
        if (CollectionUtil.isNotEmpty(copyWtxxList)){
            spcyWjWjtmxxService.saveBatch(copyWtxxList);
        }

        return HttpResult.ok();
    }

    @Log(title = "问卷题目-通过id查询")
    @ApiOperation(value = "问卷题目-通过id查询")
    @ApiImplicitParam(value = "id",name = "问卷题目id",required = true)
    @GetMapping(value = "/queryWjtmById")
    public HttpResult<?> queryWjtmById(@RequestParam("id") String id) {
        SpcyWjWjtm spcyPxSjtm = spcyWjWjtmService.getById(id);
        return HttpResult.ok(spcyPxSjtm);
    }

    /*********************【 问卷题目选项 】**********************/

    @Log(title = "问卷题目选项-分页列表查询")
    @ApiOperation(value = "问卷题目选项-分页列表查询")
    @GetMapping(value = "/wjtmxxPageList")
    public HttpResult<?> wjtmxxPageList(SpcyWjWjtmxx spcyPxSjtmxx, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SpcyWjWjtmxx> page = new Page<>(pageNo, pageSize);
        IPage<SpcyWjWjtmxx> pageList = new Page<>();
        LambdaQueryWrapper<SpcyWjWjtmxx> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(spcyPxSjtmxx.getFwjtmid())) {
            queryWrapper.eq(SpcyWjWjtmxx::getFwjtmid, spcyPxSjtmxx.getFwjtmid());
            queryWrapper.like(StringUtils.isNotEmpty(spcyPxSjtmxx.getFwjtmxx()), SpcyWjWjtmxx::getFwjtmxx, spcyPxSjtmxx.getFwjtmxx());

            queryWrapper.orderByAsc(SpcyWjWjtmxx::getFxh);
            queryWrapper.orderByAsc(SpcyWjWjtmxx::getCreateTime);
            pageList = spcyWjWjtmxxService.page(page, queryWrapper);
        }
        return HttpResult.ok(pageList);
    }


    @Log(title = "问卷题目选项-添加")
    @ApiOperation(value = "问卷题目选项-添加")
    @PostMapping(value = "/addWjtmxx")
    public HttpResult<?> addWjtmxx(@RequestBody SpcyWjWjtmxx spcyPxSjtmxx) {
        if (StringUtils.isBlank(spcyPxSjtmxx.getFwjtmid())){
            return HttpResult.error("问卷题目id不能为空");
        }
        String sjtmid = spcyPxSjtmxx.getFwjtmid();
        List<SpcyWjWjtmxx> sjtmxxList = spcyWjWjtmxxService.list(new LambdaQueryWrapper<SpcyWjWjtmxx>()
                .eq(SpcyWjWjtmxx::getFwjtmid, sjtmid));
        int size = sjtmxxList.size();
        size = size + 1;
        spcyPxSjtmxx.setFxh(size);
        spcyWjWjtmxxService.save(spcyPxSjtmxx);

        String operContent = "新增问卷题目选项信息：";
        // 将map里面的属性和值转String
        Map<String, Object> beanMap = ObjectToMapUtils.objectToMap(spcyPxSjtmxx);
        operContent += LogUtils.MapToStringMethod(beanMap);
        spcyLogService.addLogs(LogConstant.LOGS_WJ_WJJBXX,operContent,spcyPxSjtmxx.getId(), LogConstant.LOGS_WJ_WJJBXX, SpcyConstant.LOGS_SYS_TYPE_XZ);
        return HttpResult.ok("添加成功");
    }

    @Log(title = "问卷题目选项-编辑")
    @ApiOperation(value = "问卷题目选项-编辑")
    @ApiImplicitParam(name = "id",value = "问卷题目选项id",required = true)
    @PutMapping(value = "/editWjtmxx")
    public HttpResult<?> editWjtmxx(@RequestBody HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException{
        if (StringUtils.isEmpty(buffer.get("id"))){
            return HttpResult.error("试卷题目选项基本信息id不能为空");
        }
        String sjtmId = buffer.get("id");
        SpcyWjWjtmxx oldSjtmxx = spcyWjWjtmxxService.getById(sjtmId);
        SpcyWjWjtmxx oldSjtmxxUpdate = spcyWjWjtmxxService.getById(sjtmId);
        if (oldSjtmxxUpdate == null){
            return HttpResult.error("当前试卷题目基本信息数据不存在，请重试");
        }

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(oldSjtmxxUpdate, buffer);
        spcyWjWjtmxxService.updateById(oldSjtmxxUpdate);

        String operContent = "问卷题目选项基本信息修改：";
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(oldSjtmxx);
        operContent += LogUtils.BeanModifyMsg(oldSjtmxxUpdate,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_WJ_WJJBXX,operContent,sjtmId,LogConstant.LOGS_WJ_WJJBXX,SpcyConstant.LOGS_SYS_TYPE_XG);
        return HttpResult.ok("修改成功");
    }

    @Log(title = "问卷题目选项-批量删除")
    @ApiOperation(value = "问卷题目选项-批量删除")
    @DeleteMapping(value = "/deleteBatchWjtmxx")
    public HttpResult<?> deleteBatchWjtmxx(@RequestParam("ids") String ids) {
        if (StringUtils.isEmpty(ids)){
            return HttpResult.error("ids参数不能为空");
        }
        List<String> idsList = Arrays.asList(ids.split(","));
        List<SpcyWjWjtmxx> spcyRsGcyList = spcyWjWjtmxxService.listByIds(idsList);
        if (CollectionUtils.isEmpty(spcyRsGcyList)){
            return HttpResult.error("数据不存在，请重试");
        }
        String operContent = "";
        for (SpcyWjWjtmxx sjtmxx : spcyRsGcyList){
            String sjtmxxId = sjtmxx.getId(); // 问卷题目选项id
            spcyWjWjtmxxService.removeById(sjtmxxId);

            operContent = "删除问卷题目选项信息：";
            Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(sjtmxx);
            operContent += LogUtils.MapToStringMethod(oldMap);
            spcyLogService.addLogs(LogConstant.LOGS_WJ_WJJBXX,operContent,sjtmxxId,LogConstant.LOGS_WJ_WJJBXX,SpcyConstant.LOGS_SYS_TYPE_SC);
            operContent = "";

        }

        return HttpResult.ok("删除成功");
    }

    @Log(title = "问卷题目选项-通过id查询")
    @ApiOperation(value = "问卷题目选项-通过id查询")
    @ApiImplicitParam(value = "id",name = "问卷题目选项",required = true)
    @GetMapping(value = "/queryWjtmxxById")
    public HttpResult<?> queryWjtmxxById(@RequestParam("id") String id) {
        SpcyWjWjtmxx spcyPxSjtmxx = spcyWjWjtmxxService.getById(id);
        return HttpResult.ok(spcyPxSjtmxx);
    }

    /*********[ 大标题 ]**********/

    @Log(title = "问卷大标题-分页列表查询")
    @ApiOperation(value = "问卷大标题-分页列表查询")
    @GetMapping(value = "/wjDbtPageList")
    public HttpResult<?> wjDbtPageList(SpcyWjDbt wjDbt, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SpcyWjDbt> page = new Page<>(pageNo, pageSize);
        IPage<SpcyWjDbt> pageList = new Page<>();
        LambdaQueryWrapper<SpcyWjDbt> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(wjDbt.getFwjjbxxid())) {
            queryWrapper.like(SpcyWjDbt::getFwjjbxxid, wjDbt.getFwjjbxxid());
            queryWrapper.like(StringUtils.isNotEmpty(wjDbt.getFdbt()), SpcyWjDbt::getFdbt, wjDbt.getFdbt());

            queryWrapper.orderByAsc(SpcyWjDbt::getForder);
            queryWrapper.orderByAsc(SpcyWjDbt::getCreateTime);
            pageList = spcyWjDbtService.page(page, queryWrapper);
        }
        return HttpResult.ok(pageList);
    }

    @Log(title = "问卷大标题-添加")
    @ApiOperation(value = "问卷大标题-添加")
    @PostMapping(value = "/addWjDbt")
    public HttpResult<?> addWjDbt(@RequestBody SpcyWjDbt dbt) {
        if (StringUtils.isBlank(dbt.getFwjjbxxid())){
            return HttpResult.error("问卷id不能为空");
        }
        if (StringUtils.isBlank(dbt.getFdbt())){
            return HttpResult.error("大标题不能为空");
        }
        List<SpcyWjDbt> dbtList = spcyWjDbtService.list(new LambdaQueryWrapper<SpcyWjDbt>()
                .eq(SpcyWjDbt::getFwjjbxxid, dbt.getFwjjbxxid()));
        int size = dbtList.size();
        size = size + 1;
        if (CollectionUtils.isNotEmpty(dbtList)){
            for (SpcyWjDbt dbt2 : dbtList){
                if (dbt2.getFdbt().equals(dbt.getFdbt())){
                    return HttpResult.error("当前【"+dbt.getFdbt()+"】大标题，已经存在了");
                }
            }
        }
        dbt.setForder(size);
        spcyWjDbtService.save(dbt);

        String operContent = "新增问卷大标题信息：";
        // 将map里面的属性和值转String
        Map<String, Object> beanMap = ObjectToMapUtils.objectToMap(dbt);
        operContent += LogUtils.MapToStringMethod(beanMap);
        spcyLogService.addLogs(LogConstant.LOGS_WJ_WJJBXX,operContent,dbt.getFwjjbxxid(), LogConstant.LOGS_WJ_WJJBXX, SpcyConstant.LOGS_SYS_TYPE_XZ);
        return HttpResult.ok("添加成功");
    }

    @Log(title = "问卷大标题-编辑")
    @ApiOperation(value = "问卷大标题-编辑")
    @ApiImplicitParam(name = "id",value = "问卷题目选项id",required = true)
    @PutMapping(value = "/editWjDbt")
    public HttpResult<?> editWjDbt(@RequestBody SpcyWjDbt newDbt) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        if (StringUtils.isEmpty(newDbt.getId())){
            return HttpResult.error("大标题id不能为空");
        }
        SpcyWjDbt update = spcyWjDbtService.getById(newDbt.getId());
        String wjid = update.getFwjjbxxid();
        if (update == null){
            return HttpResult.error("数据不存在，请重试");
        }
        if (StringUtils.isBlank(newDbt.getFdbt())){
            return HttpResult.error("大标题不能为空");
        }

        // 前后大标题不相同
        String oldDbt = update.getFdbt(); // 旧大标题
        boolean isUpdateDbt = false;
        if (!newDbt.getFdbt().equals(oldDbt)) {
            isUpdateDbt = true;

            // 判断大题目是否重复
            List<SpcyWjDbt> wjDbtList = spcyWjDbtService.list(new LambdaQueryWrapper<SpcyWjDbt>()
                    .eq(SpcyWjDbt::getFwjjbxxid, wjid)
                    .eq(SpcyWjDbt::getFdbt, newDbt.getFdbt()));
            if (CollectionUtils.isNotEmpty(wjDbtList)){
                return HttpResult.error("当前问卷大题目【"+newDbt.getFdbt()+"】已经存在了。");
            }
        }

        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(update);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(update, BeanUtils.describe(newDbt));
        boolean flag = spcyWjDbtService.updateById(update);
        if (flag){
            String operContent = "问卷大标题修改：";
            operContent += LogUtils.BeanModifyMsg(update,oldMap);
            spcyLogService.addLogs(LogConstant.LOGS_WJ_WJJBXX,operContent,wjid,LogConstant.LOGS_WJ_WJJBXX,SpcyConstant.LOGS_SYS_TYPE_XG);

            if (isUpdateDbt){
                List<SpcyWjWjtm> wjtmList = spcyWjWjtmService.list(new LambdaQueryWrapper<SpcyWjWjtm>()
                        .eq(SpcyWjWjtm::getFwjjbxxid, wjid)
                        .eq(SpcyWjWjtm::getFwjtmdbt, oldDbt));
                if (CollectionUtils.isNotEmpty(wjtmList)) {
                    for (SpcyWjWjtm wjtm : wjtmList) {
                        wjtm.setFwjtmdbt(newDbt.getFdbt());
                        spcyWjWjtmService.updateById(wjtm);
                    }
                }
            }
        }else {
            return HttpResult.error("保存失败，请刷新重试，并联系管理员");
        }

        return HttpResult.ok("保存成功");
    }

    @Log(title = "问卷大标题-批量删除")
    @ApiOperation(value = "问卷大标题-批量删除")
    @DeleteMapping(value = "/deleteWjDbt")
    public HttpResult<?> deleteWjDbt(@RequestParam("ids") String ids) {
        if (StringUtils.isEmpty(ids)){
            return HttpResult.error("ids参数不能为空");
        }
        List<String> idsList = Arrays.asList(ids.split(","));
        List<SpcyWjDbt> spcyRsGcyList = spcyWjDbtService.listByIds(idsList);
        if (CollectionUtils.isEmpty(spcyRsGcyList)){
            return HttpResult.error("数据不存在，请重试");
        }
        String operContent = "";
        for (SpcyWjDbt dbt : spcyRsGcyList){

            boolean flag = spcyWjDbtService.removeById(dbt.getId());
            if (flag){
                // 删除大标题对应的题目
                List<SpcyWjWjtm> wjtmList = spcyWjWjtmService.list(new LambdaQueryWrapper<SpcyWjWjtm>()
                        .eq(SpcyWjWjtm::getFwjjbxxid, dbt.getFwjjbxxid())
                        .eq(SpcyWjWjtm::getFwjtmdbt, dbt.getFdbt()));
                if (CollectionUtils.isNotEmpty(wjtmList)) {
                    List<String> tmIdList = wjtmList.stream().map(SpcyWjWjtm::getId).collect(Collectors.toList());
                    spcyWjWjtmService.removeBatchByIds(tmIdList);
                }

                operContent = "删除问卷大标题信息：";
                Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(dbt);
                operContent += LogUtils.MapToStringMethod(oldMap);
                spcyLogService.addLogs(LogConstant.LOGS_WJ_WJJBXX,operContent,dbt.getFwjjbxxid(),LogConstant.LOGS_WJ_WJJBXX,SpcyConstant.LOGS_SYS_TYPE_SC);
                operContent = "";
            }

        }

        return HttpResult.ok("删除成功");
    }

    @Log(title = "通过id查询问卷详细信息")
    @ApiOperation(value = "通过id查询问卷详细信息")
    @ApiImplicitParam(value = "id",name = "问卷id",required = true)
    @GetMapping(value = "/queryWjxqxxById")
    public HttpResult<?> queryWjxqxxById(@RequestParam("id")String id){
        if (StringUtils.isEmpty(id)){
            return HttpResult.error("问卷id不能为空");
        }
        Map<String,Object> resultMap = new HashMap<>();

        SpcyWjWjjbxx sjjbxx = spcyWjWjjbxxService.getById(id);
        resultMap.put("wjjbxx",sjjbxx);

        // 题目数据
        List<SpcyWjWjtm> wjtmXList = spcyWjWjtmService.list(new LambdaQueryWrapper<SpcyWjWjtm>()
                .eq(SpcyWjWjtm::getFwjjbxxid, id)
                .orderByAsc(SpcyWjWjtm::getFxh));

        List<WjxxDbtVo> dbtMapList = new ArrayList<>();

        // 题目
        if (CollectionUtils.isNotEmpty(wjtmXList)){
            // 所有大标题
            List<String> dbtList = wjtmXList.stream().map(SpcyWjWjtm::getFwjtmdbt)
                    .distinct().collect(Collectors.toList());

            int i = 1;
            for (String dbt : dbtList){
                WjxxDbtVo wjtmdbtVo = new WjxxDbtVo();
                wjtmdbtVo.setDbt(dbt);

                List<SpcyWjWjtm> dbtTmList = spcyWjWjtmService.list(new LambdaQueryWrapper<SpcyWjWjtm>()
                        .eq(SpcyWjWjtm::getFwjjbxxid, id)
                        .eq(SpcyWjWjtm::getFwjtmdbt,dbt)
                        .orderByAsc(SpcyWjWjtm::getFxh));

                List<WjtmVo> djtmVoList = new ArrayList<>(); // 存放答卷题目和选项
                List<SpcyWjWjtmxx> djdaList = new ArrayList<>();

                if (CollectionUtils.isNotEmpty(dbtTmList)) {
                    for (SpcyWjWjtm djtm : dbtTmList) {
                        WjtmVo djtmVo = new WjtmVo();
                        BeanUtil.copyProperties(djtm, djtmVo); // 存放题目
                        // 题目选项
                        djdaList = spcyWjWjtmxxService.list(new LambdaQueryWrapper<SpcyWjWjtmxx>()
                                .eq(SpcyWjWjtmxx::getFwjtmid, djtm.getId())
                                .orderByAsc(SpcyWjWjtmxx::getFxh));
                        djtmVo.setWjtmxxList(djdaList); // 存放答案选项
                        djtmVoList.add(djtmVo);

                        wjtmdbtVo.setWjtmList(djtmVoList);
                    }
                }
                i++;
                dbtMapList.add(wjtmdbtVo);
            }
            resultMap.put("dbtList",dbtMapList);

        }

        return HttpResult.ok(resultMap);
    }

    @Log(title = "导出问卷填写的条款信息")
    @ApiOperation(value = "导出问卷填写的条款信息")
    @ApiImplicitParam(name = "id",value = "问卷基本信息id",required = true)
    @GetMapping(value = "exportWjWriteTkxxData")
    public void exportWjWriteTkxxData(@RequestParam(value = "id",defaultValue = "")String id,
                                      HttpServletResponse response){
        try {
            String name = "exportWjWriteTkxxData";
            response.setContentType("application/ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(name+".xls", "UTF-8"))));
            OutputStream out = response.getOutputStream();

//            List<String> IdsList = Arrays.asList(ids.split(","));
            List<Object> list = new ArrayList<>(); // 导出的数据
            if (StringUtils.isNotBlank(id)) {
                // 问卷填写的修订意见选项记录 - 通过问卷基本信息id
                List<SpcyWjWjtxtmxx> tmxxList = spcyWjWjjbxxMapper.findTmIsXdyjById(id);
                if (CollectionUtils.isNotEmpty(tmxxList)){
                    for (SpcyWjWjtxtmxx e : tmxxList){
                        list.add(e);
                    }
                }
            }else {
                log.error("问卷基本信息id为空");
            }
            myExcelUtils.outputListToExclFile(BusinessConstant.IMPORT_WJ_TKXX,list,out); // 导出模板数据成excel
        } catch (Exception e) {
            log.error("导出问卷填写的条款信息数据出错-{}",e.getMessage());
        }
    }

    @Log(title = "导出问卷填写的题目选项信息")
    @ApiOperation(value = "导出问卷填写的题目选项信息")
    @ApiImplicitParam(name = "id",value = "问卷基本信息id",required = true)
    @GetMapping(value = "exportWjWriteSelectxxData")
    public void exportWjWriteSelectxxData(@RequestParam(value = "id",defaultValue = "")String id,
                                      HttpServletResponse response){
        try {
            String name = "exportWjWriteSelectxxData";
            response.setContentType("application/ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(name+".xls", "UTF-8"))));
            OutputStream out = response.getOutputStream();

//            List<String> IdsList = Arrays.asList(ids.split(","));
            List<Object> list = new ArrayList<>(); // 导出的数据
            if (StringUtils.isNotBlank(id)) {

                // 问卷填写的题目选项信息 - 通过问卷基本信息id -统计
                List<WjtmStatVo> tmxxList = spcyWjWjjbxxMapper.findWjTmSelectById(id);
                if (CollectionUtils.isNotEmpty(tmxxList)){
                    for (WjtmStatVo e : tmxxList){
                        list.add(e);
                    }
                }

            }else {
                log.error("问卷基本信息id为空");
            }
            myExcelUtils.outputListToExclFile(BusinessConstant.IMPORT_WJ_TMXX,list,out); // 导出模板数据成excel
        } catch (Exception e) {
            log.error("导出问卷填写的选项信息数据出错-{}",e.getMessage());
        }
    }

//    @Log(title = "批量导出填写问卷PDF文件")
    @ApiOperation(value = "批量导出填写问卷PDF文件")
    @PostMapping(value = "exportWjFileZip")
    public void exportWjFileZip(@RequestBody ParamsBo paramsBo, HttpServletResponse response) throws IOException {
        if (StringUtils.isBlank(paramsBo.getIds())){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("参数为空！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        List<SpcyWjWjtxjbxx> wjList = spcyWjWjtxjbxxService.listByIds(StringPatternUtils.getListByIds(paramsBo.getIds()));
        this.genExportWjFileZip(wjList, true, response);

    }

//    @Log(title = "导出填写问卷详细信息文件PDF")
    @ApiOperation(value = "导出填写问卷详细信息文件PDF")
    @GetMapping(value = "exportWjFilePdf")
    public void exportWjFilePdf(@RequestParam(value = "id",defaultValue = "") String id, HttpServletResponse response) throws IOException {
        if (StringUtils.isBlank(id)){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("参数为空！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        SpcyWjWjtxjbxx wj = spcyWjWjtxjbxxService.getById(id);
        if (wj == null){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("没有相匹配的问卷数据！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        String destWordUrl = "";
        try {
            destWordUrl = this.initExportWjTemplate(wj, id);
        }catch (Exception e){
            log.error("导出问卷模板错误：{}", e.getMessage());
        }

        if (StringUtils.isBlank(destWordUrl)){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("问卷模板错误！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        // pdf保存路径
        String destPdfUrl = localFileUploadUtils.getTempFileRootUrl() + "/" + GetUuIdUtils.NotReplaceUuId() + ".pdf";
        WordPdfUtils.wordToPdf(destWordUrl, destPdfUrl);
        localFileUploadUtils.ejzLocalFileByZdyFile(new File(destPdfUrl), response);
    }

    /** 初始化问卷模板 */
    private String initExportWjTemplate(SpcyWjWjtxjbxx wj, String id){
        HashMap<String, Object> parMap = new HashMap<>();

        /** 问卷基本信息 */
        parMap.put("fwjbt", wj.getFwjbt());
        parMap.put("fwjms", wj.getFwjms());
        parMap.put("fsfdwmc", wj.getFsfdwmc());
        parMap.put("fsfdwdz", wj.getFsfdwdz());
        parMap.put("flxr", wj.getFlxr());
        parMap.put("flxdh", wj.getFlxdh());
        parMap.put("ftxrq", wj.getFtxrq() != null ? DateConversionUtils.DateToYMDHMSChineseString(wj.getFtxrq()) : "");
        parMap.put("ftjrq", wj.getFtjrq() != null ? DateConversionUtils.DateToYMDHMSChineseString(wj.getFtjrq()) : "");

        /** 问卷大标题 */
        List<Map<String, Object>> dbtListMap = new ArrayList<>(); // 大标题、题目及选项
        List<String> dbtList = new ArrayList<>();
        List<SpcyWjDbt> orgDbtList = spcyWjDbtService.list(new LambdaQueryWrapper<SpcyWjDbt>()
                .eq(SpcyWjDbt::getFwjjbxxid, id)
                .orderByAsc(SpcyWjDbt::getForder));
        if (CollectionUtil.isNotEmpty(orgDbtList)){
            dbtList = orgDbtList.stream().map(SpcyWjDbt::getFdbt).collect(Collectors.toList());
        }else {
            List<SpcyWjWjtxtm> djtmDanxList = spcyWjWjtxtmService.list(new LambdaQueryWrapper<SpcyWjWjtxtm>()
                    .eq(SpcyWjWjtxtm::getFwjtxjbxxid, id)
                    .orderByAsc(SpcyWjWjtxtm::getFxh));
            dbtList = djtmDanxList.stream().map(SpcyWjWjtxtm::getFwjtmdbt)
                    .distinct().collect(Collectors.toList());
        }

        /** 组织问卷题目选项 */
        for (String dbt : dbtList) {
            Map<String, Object> dbrMap = new HashMap<>();

            /** 大标题 */
            dbrMap.put("dbt", dbt);

            // 题目及选项数据
            List<Map<String, Object>> questionsListMap = new ArrayList<>();

            /** 问卷题目 */
            List<SpcyWjWjtxtm> tmList = spcyWjWjtxtmService.list(new LambdaQueryWrapper<SpcyWjWjtxtm>()
                    .eq(SpcyWjWjtxtm::getFwjtxjbxxid, id)
                    .eq(SpcyWjWjtxtm::getFwjtmdbt,dbt)
                    .orderByAsc(SpcyWjWjtxtm::getFxh));
            for (SpcyWjWjtxtm tm : tmList) {
                Map<String, Object> questionMap = new HashMap<>();

                /** 题目相关信息 */
                if (StringUtils.isNotBlank(tm.getFwjtm()) && tm.getFxh() != null
                        && tm.getFwjtm().startsWith(String.valueOf(tm.getFxh()))){
                    questionMap.put("question", tm.getFwjtm());
                }else {
                    questionMap.put("question", tm.getFxh()+"、 "+tm.getFwjtm());
                }
//                questionMap.put("question", tm.getFxh() + "、" + tm.getFwjtm());
                questionMap.put("fczkn", Optional.ofNullable(tm.getFczkn()).orElseGet(() -> ""));
                questionMap.put("fyjjy", Optional.ofNullable(tm.getFyjjy()).orElseGet(() -> ""));
                questionMap.put("fyxdz", Optional.ofNullable(tm.getFyxdz()).orElseGet(() -> ""));
                questionMap.put("fpfjg", Optional.ofNullable(tm.getFpfjg()).orElseGet(() -> ""));


                /** 题目选项 ☑ □ */
                String answer = "";
                List<SpcyWjWjtxtmxx> xxList = spcyWjWjtxtmxxService.list(new LambdaQueryWrapper<SpcyWjWjtxtmxx>()
                        .eq(SpcyWjWjtxtmxx::getFwjtxtmid, tm.getId())
                        .orderByAsc(SpcyWjWjtxtmxx::getFxh));
                for (SpcyWjWjtxtmxx xx : xxList) {
                    answer += StringUtils.isNotBlank(answer) ? " " : "";
                    if (StringUtils.isNotBlank(xx.getFsfxz()) && xx.getFsfxz().equals("是")) {
                        answer += "√" + xx.getFwjtmxx();
                    } else {
                        answer += "□" + xx.getFwjtmxx();
                    }
                }
                questionMap.put("answer", answer);

                questionsListMap.add(questionMap);
            }

            dbrMap.put("questions", questionsListMap);

            dbtListMap.add(dbrMap);
        }
        parMap.put("dbtList", dbtListMap);

        String orgUrl = businessConstant.UPLOAD_FILE_ROOT + "/" + WjmlConstant.Template_File_Export_Wjtxxx;
        String destWordUrl = localFileUploadUtils.getTempFileRootUrl() + "/";
        destWordUrl += GetUuIdUtils.NotReplaceUuId() + Util.getWjHz(orgUrl);

        ConfigureBuilder builder = Configure.builder();
        WordDocxReplaceUtils.docxConfigureReplace(orgUrl, destWordUrl, builder.build(), parMap);

        return destWordUrl;
    }

    /**
     * 批量导出填写问卷PDF文件
     * @param isPdf true: pdf
     * */
    private void genExportWjFileZip(List<SpcyWjWjtxjbxx> wjList, boolean isPdf, HttpServletResponse response){
        List<InputStream> files = new ArrayList<>();
        List<String> fileNames = new ArrayList();
        String destWordUrl = "";
        String wjccmc = "";
        String outFileUrl = "";
        String fileName = "";

        File fileMkdir = new File(localFileUploadUtils.getTempFileRootUrl() + "/" + DateConversionUtils.DateToMonthTwo(new Date()));
        if (!fileMkdir.exists()){
            fileMkdir.mkdirs();
        }

        for (SpcyWjWjtxjbxx wj : wjList){
            destWordUrl = "";
            wjccmc = "";
            outFileUrl = "";
            fileName = "";

            try {
                destWordUrl = this.initExportWjTemplate(wj, wj.getId());
            }catch (Exception e){
                log.error("导出问卷模板错误：{}", e.getMessage());
            }

            if (StringUtils.isBlank(destWordUrl)){
                continue;
            }

            if (isPdf) {
                /** pdf保存路径 */
                wjccmc = DateConversionUtils.DateToMonthTwo(new Date()) + "/" + GetUuIdUtils.NotReplaceUuId() + ".pdf";
                outFileUrl = localFileUploadUtils.getTempFileRootUrl() + "/" + wjccmc;
                WordPdfUtils.wordToPdf(destWordUrl, outFileUrl);
            }else {
                outFileUrl = destWordUrl;
            }

            fileName += StringUtils.isNotBlank(wj.getFwjbt()) ? "("+wj.getFwjbt()+")" : "";
            fileName += StringUtils.isNotBlank(wj.getFsfdwmc()) ? (StringUtils.isNotBlank(fileName) ? "-"+wj.getFsfdwmc() : wj.getFsfdwmc()) : "";
            fileName += StringUtils.isNotBlank(wj.getFlxr()) ? (StringUtils.isNotBlank(fileName) ? "-"+wj.getFlxr() : wj.getFlxr()) : "";
            fileName += wj.getFtxrq() != null ? (StringUtils.isNotBlank(fileName) ? "-"+DateConversionUtils.DateToDays(wj.getFtxrq()) : DateConversionUtils.DateToDays(wj.getFtxrq())) : "";

            try {
                files.add(new FileInputStream(outFileUrl));
                if (isPdf) {
                    fileNames.add(fileName + ".pdf");
                }else {
                    fileNames.add(fileName + ".docx");
                }
            } catch (FileNotFoundException e) {

            }
        }

        PdfUtils.downInputZip(files, fileNames, response);
    }

}
